/**
 * The contents of this file are subject to the OpenMRS Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://license.openmrs.org
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) OpenMRS, LLC.  All Rights Reserved.
 */

package org.openmrs.module.clinicalsummary.rule;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Concept;
import org.openmrs.Encounter;
import org.openmrs.EncounterType;
import org.openmrs.Location;
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.PatientIdentifier;
import org.openmrs.PatientIdentifierType;
import org.openmrs.PersonAddress;
import org.openmrs.PersonName;
import org.openmrs.api.context.Context;

import java.util.Date;
import java.util.Set;

public final class EvaluableReminderUtils {

	private static final Log log = LogFactory.getLog(EvaluableReminderUtils.class);

	public static final String GENDER_MALE = "M";

	public static final String GENDER_FEMALE = "F";

	/**
	 * Utility method to crete obs for testing
	 *
	 * @param encounter
	 * @param concept
	 * @param valueCoded
	 * @param valueNumeric
	 * @param valueText
	 * @return
	 */
	public static Obs createObs(final Encounter encounter, final Concept concept, final Concept valueCoded,
	                            final Double valueNumeric, final String valueText) {
		Obs obs = new Obs();
		obs.setConcept(concept);
		obs.setEncounter(encounter);
		obs.setDateCreated(encounter.getEncounterDatetime());
		obs.setLocation(Context.getLocationService().getLocation(1));
		obs.setObsDatetime(encounter.getEncounterDatetime());
		obs.setPerson(encounter.getPatient());
		obs.setValueCoded(valueCoded);
		obs.setValueNumeric(valueNumeric);
		obs.setValueText(valueText);

		encounter.addObs(obs);
		return Context.getObsService().saveObs(obs, "Testing");
	}

	/**
	 * Utility method to create encounter for testing
	 *
	 * @param patient
	 * @param encounterType
	 * @param encounterDatetime
	 * @return
	 */
	public static Encounter createEncounter(final Patient patient, final EncounterType encounterType, final Date encounterDatetime) {
		Encounter encounter = new Encounter();
		encounter.setLocation(Context.getLocationService().getLocation(1));
		encounter.setEncounterType(encounterType);
		encounter.setEncounterDatetime(encounterDatetime);
		encounter.setPatient(patient);
		encounter.setProvider(Context.getAuthenticatedUser().getPerson());
		return Context.getEncounterService().saveEncounter(encounter);
	}

	/**
	 * Create patient stub for testing
	 * @return
	 * @param gender
	 * @param birthdate
	 */
	public static Patient createPatient(final String gender, final Date birthdate) {
		Patient patient = new Patient();

		Location location = Context.getLocationService().getLocation(1);
		PatientIdentifierType identifierType = Context.getPatientService().getPatientIdentifierType(1);
		PatientIdentifier identifier = new PatientIdentifier();
		identifier.setIdentifierType(identifierType);
		identifier.setIdentifier("999-3");
		identifier.setLocation(location);
		patient.addIdentifier(identifier);

		PersonName name = new PersonName();
		name.setGivenName("Tom");
		name.setMiddleName("E.");
		name.setFamilyName("Patient");
		patient.addName(name);

		PersonAddress address = new PersonAddress();
		address.setAddress1("123 My street");
		address.setAddress2("Apt 402");
		address.setCityVillage("Anywhere city");
		address.setCountry("Some Country");
		Set<PersonAddress> addresses = patient.getAddresses();
		addresses.add(address);
		patient.setAddresses(addresses);
		patient.addAddress(address);

		patient.setBirthdate(new Date());
		patient.setBirthdateEstimated(true);
		patient.setGender(EvaluableReminderUtils.GENDER_MALE);

		return Context.getPatientService().savePatient(patient);
	}
}
